/**
 * Copyright 2022 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_SPARSE_TENSOR_DENSE_ADD_GPU_KERNEL_H_
#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_SPARSE_TENSOR_DENSE_ADD_GPU_KERNEL_H_

#include <cuda_runtime_api.h>
#include <cusparse.h>
#include <vector>
#include <string>
#include <map>
#include <utility>
#include <algorithm>
#include "kernel/gpu/gpu_kernel.h"
#include "kernel/gpu/gpu_kernel_factory.h"
#include "kernel/gpu/kernel_constants.h"
#include "kernel/gpu/cuda_impl/cuda_ops/sparse_tensor_dense_add_impl.cuh"

namespace mindspore {
namespace kernel {
constexpr auto kUnknown = "Unknown";

class SparseTensorDenseAddGpuKernelMod : public NativeGpuKernelMod {
 public:
  SparseTensorDenseAddGpuKernelMod() { ResetResource(); }
  ~SparseTensorDenseAddGpuKernelMod() override = default;

  bool Launch(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &workspace,
              const std::vector<KernelTensor *> &outputs, void *cuda_stream) override {
    cuda_stream_ = cuda_stream;
    return kernel_func_(this, inputs, workspace, outputs);
  }

  bool Init(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &outputs) override;
  int Resize(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &outputs) override;
  std::vector<KernelAttr> GetOpSupport() override;
  void ResetResource() noexcept {
    is_null_output_ = false;
    x2_shape_size = 0;
    input_elements_ = 0;
    workspace_size_ = 0;
    output_elements_ = 1;
    workspace_size_list_.clear();
  }

 protected:
  void InitSizeLists() {
    workspace_size_list_.clear();

    // The workspace size
    workspace_size_ = x2_shape_size * sizeof(size_t);
    workspace_size_list_.push_back(workspace_size_);
  }

 private:
  template <typename T, typename I>
  bool LaunchKernel(const std::vector<kernel::KernelTensor *> &inputs,
                    const std::vector<kernel::KernelTensor *> &workspace,
                    const std::vector<kernel::KernelTensor *> &outputs);

  using SparseTensorDenseAddLaunchFunc =
    std::function<bool(SparseTensorDenseAddGpuKernelMod *, const std::vector<kernel::KernelTensor *> &,
                       const std::vector<kernel::KernelTensor *> &, const std::vector<kernel::KernelTensor *> &)>;
  static std::vector<std::pair<KernelAttr, SparseTensorDenseAddLaunchFunc>> func_list_;
  SparseTensorDenseAddLaunchFunc kernel_func_;
  void *cuda_stream_{nullptr};
  std::vector<int64_t> x2_shape_;
  size_t unit_size_{1};
  size_t input_elements_{};
  size_t output_elements_{1};
  size_t rank_{};
  size_t x2_shape_size;
  size_t workspace_size_;
  bool is_null_output_;
};
}  // namespace kernel
}  // namespace mindspore

#endif  // MINDSPORE_CCSRC_PLUGIN_DEVICE_GPU_KERNEL_SPARSE_SPARSE_TENSOR_DENSE_ADD_GPU_KERNEL_H_
